<?php

/**
 * This is the model class for table "{{moudle}}".
 *
 * The followings are the available columns in table '{{moudle}}':
 * @property string $ModuleID
 * @property string $Title
 * @property string $AddTime
 * @property string $Brief
 * @property integer $Status
 * @property string $Sorting
 */
class ModuleModel extends BaseModel
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{module}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('Title', 'required'),
			array('Status,IsIndex', 'numerical', 'integerOnly'=>true),
			array('TagIDs,TagListIDs', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('Title, AddTime, Brief, Status, Sorting,TagIDs,IsIndex', 'safe', 'on'=>'admin'),
			array('ModuleID, Title, AddTime, Brief, Status, Sorting,IsIndex', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ModuleID' => '模块ID',
			'Title' => '标题',
			'AddTime' => '添加时间',
			'Brief' => '简介',
			'Status' => '状态',
			'IsIndex' => '是否展示在首页上',
			'TagIDs' => '展示在首页分类标签列表',
			'TagListIDs' => '展示在二级页面的标签列表',
			'Sorting' => '排序',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('ModuleID',$this->ModuleID,true);
		$criteria->compare('Title',$this->Title,true);
		$criteria->compare('AddTime',$this->AddTime,true);
		$criteria->compare('Brief',$this->Brief,true);
		$criteria->compare('Status',$this->Status);
		$criteria->compare('Sorting',$this->Sorting,true);
		$criteria->compare('TagIDs',$this->TagIDs,true);
		$criteria->compare('TagListIDs',$this->TagListIDs,true);
		$criteria->compare('IsIndex',$this->IsIndex);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function createSearchCriteria()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('ModuleID',$this->ModuleID,true);
		$criteria->compare('Title',$this->Title,true);
		$criteria->compare('AddTime',$this->AddTime,true);
		$criteria->compare('Brief',$this->Brief,true);
		$criteria->compare('Status',$this->Status);
		$criteria->compare('Sorting',$this->Sorting,true);
		$criteria->compare('TagIDs',$this->TagIDs,true);
		$criteria->compare('TagListIDs',$this->TagListIDs,true);
		$criteria->compare('IsIndex',$this->IsIndex);

		return $criteria;
	}
	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return MoudleModel the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public static function getList($bloIsCache = true){
		$strKey = "MoudleModel_getList";
		$arrCache = yii::app()->cache->get($strKey);
        if($bloIsCache && $arrCache !== false){
            return $arrCache;
        }
        $arrCache = array();
        $objCriteria = BaseModel::getC(array('Status' => 1));
        $objCriteria->order = 'Sorting DESC,AddTime DESC';
        $objModels = self::model()->findAll();
        foreach ($objModels as $key => $objModel) {
        	$arrCache[$objModel->ModuleID] = $objModel->attributes;
        }
        
        yii::app()->cache->set($strKey, $arrCache, 600);
        return $arrCache;			
	}

	/**
	 * 找在首页显示的模块
	 * @return [type] [description]
	 */
	public static function getIndexList(){
		$arrResult = array();
		$arrDatas = self::getList();
		foreach ($arrDatas as $key => $arrData) {
			if($arrData['IsIndex'] == 1){
				$arrResult[$key] = $arrData;
			}
		}
		return $arrResult;
	}


	/**
	 * 状态
	 * @return [type] [description]
	 */
	public static function getStatusHtml($mixData = false){
		$arrData = array(
			0 => '不可用',
			1 => '可用',			
		);
		if($mixData !== false){
			return $arrData[$mixData];
		}else{
			return $arrData;
		}
	}

	public function beforeSave(){
		if($this->isNewRecord){
			$this->AddTime = time();
		}
		return true;
	}

	public function afterSave(){
		$this->getList(false);
	}
	public function afterDelete(){
		ModuleGoodsModel::model()->deleteAll(BaseModel::getC(array('ModuleID' => $this->ModuleID)));	
		$this->getList(false);
	}


	/**
	 * 获取全部的信息，主键 对应你 传过来的字段
	 * @param  [type] $strField [description]
	 * @return [type]           [description]
	 */
	public static function getFieldList($strField){
		$arrDatas = self::getList();
		$arrResult = array();
		foreach ($arrDatas as $ModuleID => $arrData) {
			$arrResult[$ModuleID] = $arrData[$strField];
		}

		return $arrResult;
	}
	//通过主键ID来获取这条记录，，，如果有加字段则获取这个字段的值
	public static function getOne($ModuleID, $strField = ''){
		$data = self::getList();
		if($strField){
			return $data[$ModuleID][$strField];
		}else{
			return $data[$ModuleID];
		}
	}

	

}
